package com.example.event_resource_java.commons.base;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页显示的标准类,基本操作,是先给予-当前页数一共的数据条数-每页显示的条数, 然后在初始化该类,得到总共页数,和开始序号和结束序号,
 * 然后数据库分页用到开始序号和结束序号,得到数据集合后赋值给该类的list属性,
 *
 *
 *
 * @author admin
 *
 * @param <T>
 */
public class PageVo<T> {
    private int pageNum;// 当前页数

    private int pageTotal;// 一共的页数

    private int total;// 数据条数

    private int pageSize;// 每页的数据条数

    private int start;// 起始数据位置

    private int end;// 结束

    private List<T> list = new ArrayList<T>();

    //放在拦截器里面做了
    public void init() {
        /*
         * 根count 和pageCount计算页数pageSize
         */
        int pageSize_x = total / pageSize;
        if (total >= pageSize) {
            this.pageTotal = total % pageSize == 0 ? pageSize_x : pageSize_x + 1;
        } else {
            this.pageTotal = 1;
        }
        // 判断页数和当前页数
        if (pageNum > pageTotal) {
            pageNum = pageTotal;
        }
        if (pageNum < 1) {
            pageNum = 1;
        }
        // 根据当前页计算起始和结束条目
        this.start = (pageNum - 1) * pageSize;
        this.end = pageNum * pageSize-1;
    }

    public PageVo(int pageNum, int total, int pageSize) {
        super();
        this.pageNum = pageNum;
        this.total = total;
        this.pageSize = pageSize;
    }

    /**
     * wjp
     * 新增入参
     * @param filter 过滤条件
     * @param sort 排序
     * @param pagination 分页
     * @param pageNum 当前页数
     * @param pageSize 页面上的数据条数
     */
    public PageVo(String filter, String sort, boolean pagination,
                  int pageNum, int pageSize){
        super();
        this.pageNum = pageNum;
        this.pageSize = pageSize;
//		if (StringUtils.isNotEmpty(sort)) {
//			this.sortBy = sort;
//		}
    }

    public PageVo(int pageNum, int total, int pageSize, List<T> list) {
        super();
        this.pageNum = pageNum;
        this.total = total;
        this.pageSize = pageSize;
        this.list = list;
    }

    public PageVo() {
        super();

    }

    @Override
    public String toString() {
        return "PageVo [total=" + total + ", end=" + end + ", list=" + list + ", pageSize=" + pageSize + ", pageNum=" + pageNum + ", pageTotal=" + pageTotal + ", start=" + start + "]";
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageTotal() {
        return pageTotal;
    }

    public void setPageTotal(int pageTotal) {
        this.pageTotal = pageTotal;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

}
